package com.alibaba.pet.service.impl;

import com.alibaba.basic.domain.Point;
import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.util.BaiduAuditUtils;
import com.alibaba.basic.util.DistanceUtil;
import com.alibaba.basic.util.LoginContext;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.org.mapper.ShopMapper;
import com.alibaba.pet.domain.SearchMasterMsg;
import com.alibaba.pet.domain.SearchMasterMsgAuditLog;
import com.alibaba.pet.mapper.PetMapper;
import com.alibaba.pet.mapper.SearchMasterMsgAuditLogMapper;
import com.alibaba.pet.mapper.SearchMasterMsgMapper;
import com.alibaba.pet.service.ISearchMasterMsgService;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.domain.User;
import com.alibaba.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private PetMapper petMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //获取发布寻主消息的用户
        Logininfo logininfo = LoginContext.getLoginUser(request); //解密
        User user = userMapper.loadByLogininfoID(logininfo.getId());
        //添加寻主消息
        searchMasterMsg.setUserId(user.getId()); //将查询到的用户id 设置到searchMasterMsg对象中
        searchMasterMsgMapper.save(searchMasterMsg); //将有user.getId()的对象设置到searchMasterMsg对象中添加到数据中
        //智能审核
        Boolean textBoo = BaiduAuditUtils.TextCensor(searchMasterMsg.getTitle() + searchMasterMsg.getAddress() + searchMasterMsg.getName());
//        Boolean imageBoo = BaiduAuditUtils.ImgCensor(searchMasterMsg.getResources());
        if (textBoo ) {
            //2.1.审核通过： 修改寻主消息状态1【审核通过待处理】 + 将当前寻主消息分配给最近的门店 + 发短信告诉它 + 添加审核日志
            //设置状态和日志
            searchMasterMsg.setState(1);
            searchMasterMsg.setNote("审核成功");
            //绑定店铺
            Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
            //50公里以内
            Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());


            if (nearestShop!=null) {
                searchMasterMsg.setShopId(nearestShop.getId());
                //模拟邮件发送短信
                System.out.println("您有新的订单需要处理" + searchMasterMsg.getId());
            }
            searchMasterMsgMapper.update(searchMasterMsg);

            //无论附近是否有店铺  都需要记录审核日志(log)
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(searchMasterMsg.getId());
            log.setState(1);
            log.setNote("审核成功,祝贺！");
            searchMasterMsgAuditLogMapper.save(log);
        } else {
            //修改状态和日志
            searchMasterMsg.setState(0);
            searchMasterMsg.setNote("审核失败，内容非法");
            searchMasterMsgMapper.update(searchMasterMsg);

            //2.2.审核失败：修改寻主消息状态0 + 添加审核日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(searchMasterMsg.getId());
            //audit_id - 自动审核，没有添加审核人
            log.setState(0);//驳回
            log.setNote("审核失败，内容非法");
            searchMasterMsgAuditLogMapper.save(log);
            throw new BusinessException("审核失败，内容非法");
        }
    }

}
